package pe.gob.sedapal.scmultimedia.rcp.view.estadocuenta;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

import pe.gob.sedapal.scmultimedia.config.ApplicationContext;
import pe.gob.sedapal.scmultimedia.domain.Suministro;
import pe.gob.sedapal.scmultimedia.service.EstadoCuentaService;
import pe.gob.sedapal.scmultimedia.sessionbean.DataSessionBean;
import pe.gob.sedapal.scmultimedia.util.Constante;
import pe.gob.sedapal.scmultimedia.util.Messages;
import pe.gob.sedapal.scmultimedia.wb.swt.ResourceManager;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.wb.swt.SWTResourceManager;

public class BuscarSuministroView extends ViewPart {
	
	public static final String ID = Constante.ID_BUSCAR_SUMINISTRO_VIEW;
	private Text txtSuministro;
	
	private Long numero=0L;
	private DataSessionBean dataSessionBean;
	private EstadoCuentaService estadoCuentaService;
	
	public BuscarSuministroView() {
		dataSessionBean = (DataSessionBean)ApplicationContext.getBean(DataSessionBean.class);
		estadoCuentaService = (EstadoCuentaService)ApplicationContext.getBean(EstadoCuentaService.class);
	}
	
	@Override
	public void createPartControl(final Composite parent) {
		
		parent.setBackgroundImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_FONDO_SEDAPAL));
		parent.setBackgroundMode(SWT.INHERIT_DEFAULT);
		parent.setLayout(null);
		
		txtSuministro = new Text(parent, SWT.BORDER | SWT.READ_ONLY | SWT.RIGHT);
		txtSuministro.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		txtSuministro.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Ubuntu", 18, SWT.BOLD));
		txtSuministro.setText("");
		txtSuministro.setBounds(45, 97, 178, 51);
		
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setBounds(45, 154, 289, 371);
		composite.setBackgroundImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_FONDO_TECLADO));
		composite.setBackgroundMode(SWT.INHERIT_DEFAULT);
		
		Label imgLogo = new Label(parent, SWT.NONE);
		imgLogo.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_LOGO_SEDAPAL_INTERNO));
		imgLogo.setBounds(635, 10, 192, 84);
		
		Label imgBarralbl = new Label(parent, SWT.NONE);
		imgBarralbl.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BARRA));
		imgBarralbl.setBounds(10, 61, 736, 8);
		
		final Label boton1 = new Label(composite, SWT.NONE);
		boton1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				numero = numero*10+1;
				setearNumeroSuministro();
			}
		});
		boton1.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				boton1.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_1_OFF));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				boton1.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_1_ON));
			}
		});
		boton1.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_1_ON));
		boton1.setBounds(10, 10, 85, 85);
		
		
		final Label boton2 = new Label(composite, SWT.NONE);
		boton2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				numero = numero*10+2;
				setearNumeroSuministro();
			}
		});
		boton2.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				boton2.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_2_OFF));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				boton2.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_2_ON));
			}
		});
		boton2.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_2_ON));
		boton2.setBounds(101, 10, 85, 85);
		
		
		
		
		
		final Label boton3 = new Label(composite, SWT.NONE);
		boton3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				numero = numero*10+3;
				setearNumeroSuministro();
			}
		});
		boton3.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				boton3.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_3_OFF));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				boton3.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_3_ON));
			}
		});
		boton3.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_3_ON));
		boton3.setBounds(192, 10, 85, 85);
		
		
		
		
		final Label boton4 = new Label(composite, SWT.NONE);
		boton4.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				numero = numero*10+4;
				setearNumeroSuministro();
			}
		});
		boton4.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				boton4.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_4_OFF));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				boton4.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_4_ON));
			}
		});
		boton4.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_4_ON));
		boton4.setBounds(10, 101, 85, 85);
	
		
		
		
		final Label boton5 = new Label(composite, SWT.NONE);
		boton5.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				numero = numero*10+5;
				setearNumeroSuministro();
			}
		});
		boton5.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				boton5.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_5_OFF));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				boton5.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_5_ON));
			}
		});
		boton5.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_5_ON));
		boton5.setBounds(101, 101, 85, 85);
		
		
		
		
		
		
		final Label boton6 = new Label(composite, SWT.NONE);
		boton6.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				numero = numero*10+6;
				setearNumeroSuministro();
			}
		});
		boton6.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				boton6.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_6_OFF));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				boton6.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_6_ON));
			}
		});
		boton6.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_6_ON));
		boton6.setBounds(192, 101, 85, 85);
		
		
		
		
		final Label boton7 = new Label(composite, SWT.NONE);
		boton7.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				numero = numero*10+7;
				setearNumeroSuministro();
			}
		});
		boton7.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				boton7.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_7_OFF));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				boton7.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_7_ON));
			}
		});
		boton7.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_7_ON));
		boton7.setBounds(10, 192, 85, 85);
		
		
		
		
		
		
		final Label boton8 = new Label(composite, SWT.NONE);
		boton8.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				numero = numero*10+8;
				setearNumeroSuministro();
			}
		});
		boton8.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				boton8.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia",Constante.RUTA_BOTON_8_OFF));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				boton8.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_8_ON));
			}
		});
		boton8.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_8_ON));
		boton8.setBounds(101, 192, 85, 85);
		
		
		
		
		
		final Label boton9 = new Label(composite, SWT.NONE);
		boton9.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				numero = numero*10+9;
				setearNumeroSuministro();
			}
		});
		boton9.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				boton9.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_9_OFF));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				boton9.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_9_ON));
			}
		});
		boton9.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_9_ON));
		boton9.setBounds(192, 192, 85, 85);
		
		
		
		
		final Label boton0 = new Label(composite, SWT.NONE);
		boton0.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				numero = numero*10;
				setearNumeroSuministro();
			}
		});
		boton0.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				boton0.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_0_OFF));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				boton0.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_0_ON));
			}
		});
		boton0.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_0_ON));
		boton0.setBounds(10, 283, 85, 85);
		
		final Label btnLimpiar = new Label(composite, SWT.NONE);
		btnLimpiar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				numero = 0L;
				txtSuministro.setText("");
			}
		});
		btnLimpiar.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				btnLimpiar.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_LIMPIAR_ON));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				btnLimpiar.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_LIMPIAR_OFF));
			}
		});
		btnLimpiar.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_LIMPIAR_OFF));
		btnLimpiar.setBounds(101, 283, 180, 85);
		
		Label lblBsquedaPorNmero = new Label(parent, SWT.NONE);
		lblBsquedaPorNmero.setForeground(SWTResourceManager.getColor(51, 102, 153));
		lblBsquedaPorNmero.setFont(SWTResourceManager.getFont("FreeSans", 17, SWT.BOLD | SWT.ITALIC));
		lblBsquedaPorNmero.setBounds(45, 25, 267, 30);
		lblBsquedaPorNmero.setText(Messages.buscarSuministro_titulo);
		
		final Label imgBorrarDigito = new Label(parent, SWT.NONE);
		imgBorrarDigito.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				numero = numero/10;
				if(numero>0){
					txtSuministro.setText(numero.toString());
				}else{
					txtSuministro.setText("");
				}
			}
		});
		imgBorrarDigito.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				imgBorrarDigito.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_BORRAR_DIGITO_OFF));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				imgBorrarDigito.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_BORRAR_DIGITO_ON));
			}
		});
		imgBorrarDigito.setAlignment(SWT.CENTER);
		imgBorrarDigito.setBounds(229, 97, 105, 51);
		imgBorrarDigito.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_BORRAR_DIGITO_OFF));
		
		final Label lblAceptar = new Label(parent, SWT.NONE);
		lblAceptar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				
				if(numero>1000000 && numero < 9999999){
					dataSessionBean.getSuministro().setNis(numero);
					Suministro sum = estadoCuentaService.obtenerSuministro(dataSessionBean.getSuministro().getNis());
					if(sum != null){
						IWorkbenchPage pagina = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
						IViewReference vistaActual = pagina.findViewReference(BuscarSuministroView.ID);
						
						pagina.hideView(vistaActual);
						
						try{
							pagina.showView(ResultadoView.ID);
						}catch(Exception ex){
							ex.printStackTrace();
						}						
					}else{
						MessageDialog.openInformation(parent.getShell(), Constante.MSG_ERROR, Constante.MSG_ERROR_NUMERO_DE_SUMINISTRO_NO_EXISTE);
					}

				}else{
					MessageDialog.openInformation(parent.getShell(), Constante.MSG_ERROR, Constante.MSG_ERROR_NUMERO_DE_SUMINISTRO_7_NUMEROS);
				}
				
			}
		});
		lblAceptar.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				lblAceptar.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_ACEPTAR_OFF));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				lblAceptar.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_ACEPTAR_ON));
			}
		});
		lblAceptar.setAlignment(SWT.CENTER);
		lblAceptar.setBounds(392, 442, 162, 63);
		lblAceptar.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_ACEPTAR_OFF));
		
		final Label lblCancelar = new Label(parent, SWT.NONE);
		lblCancelar.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				lblCancelar.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_REGRESAR_OFF));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				lblCancelar.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_REGRESAR_ON));
			}
		});
		lblCancelar.setAlignment(SWT.CENTER);
		lblCancelar.setBounds(561, 442, 174, 63);
		lblCancelar.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_REGRESAR_OFF));
		
		Label imgMuestraSum = new Label(parent, SWT.NONE);
		imgMuestraSum.setBounds(365, 154, 381, 282);
		imgMuestraSum.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_MUESTRA_SUMINISTRO));
		
		Label lblPorNumero = new Label(parent, SWT.NONE);
		lblPorNumero.setFont(SWTResourceManager.getFont("FreeSans", 12, SWT.BOLD | SWT.ITALIC));
		lblPorNumero.setForeground(SWTResourceManager.getColor(51, 102, 153));
		lblPorNumero.setBounds(318, 38, 289, 17);
		lblPorNumero.setText(Messages.buscarSuministro_subtitulo);
		
		lblCancelar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				IWorkbenchPage pagina = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				IViewReference vistaActual = pagina.findViewReference(BuscarSuministroView.ID);
				
				pagina.hideView(vistaActual);
				
				try{
					pagina.showView(ConsultaEstadoCuentaView.ID);
				}catch(Exception ex){
					ex.printStackTrace();
				}				
			}
		});
		
	}
	
	public void setearNumeroSuministro(){
		if(numero>9999999){
			numero = numero/10;
		}
		txtSuministro.setText(numero.toString());
	}
	
	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
}
